package com.shiguiwu.springmybatis.spring.aop.cglib;

import com.shiguiwu.springmybatis.spring.autowired.B;
import com.shiguiwu.springmybatis.spring.autowired.C;
import lombok.Data;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.LazyLoader;

import java.util.UUID;

/**
 * @description: 懒加载
 *
 * LazyLoader实现延迟加载
 * @author: stone
 * @date: Created by 2021/5/18 20:38
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.aop.cglib
 */
public class LazyLoaderPlusTests {

//    @Data
    public static class Blog {
        private String title;
        private Content content;

        public Blog() {
            this.title = "spring AOP";
            this.content = this.getContent();

        }

        private Content getContent() {
            Enhancer enhancer = new Enhancer();

            enhancer.setSuperclass(Content.class);

            enhancer.setCallback(new LazyLoader() {
                @Override
                public Object loadObject() throws Exception {
                    System.out.println("调用loadObject方法");
                    System.out.println("数据库正在开始获取blog内容。。。。。。。。");
                    Content content = new Content();
                    content.setContent("欢迎大家和我一起学些spring，我们一起成为spring高");
                    return content;
                }

            });
            Object proxy = enhancer.create();

            return (Content) proxy;
        }

    }

    @Data
    public static class Content {

        private String content;



    }


    public static void main(String[] args) {
        Blog blog = new Blog();
        System.out.println(blog.title);
//        System.out.println(blog.getContent());
        System.out.println("博客内容。。。。。。");
        System.out.println(blog.getContent().getContent());

    }


}
